package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    RoleDao roleDao;
    @Autowired
    PermissionDao permissionDao;

    @Override
    public User findUserByName(String username) {
        //1.根据用户名查询用户
        User user = userDao.findUserByName(username);
        if (user == null) {
            return null;
        }
        //2.根据用户id查询用户所拥有的角色
        Integer userId = user.getId();
        Set<Role> roleSet = roleDao.findRoleByUserId(userId);
        user.setRoles(roleSet);

        for (Role role : roleSet) {
            //3.根据角色id查询角色拥有的权限
            Integer roleId = role.getId();
            Set<Permission> permissionSet = permissionDao.findPermissionByRoleId(roleId);
            role.setPermissions(permissionSet);
        }

        return user;
    }
}
